package com.ginmeta.common.utils.security;

import cn.hutool.core.util.CharsetUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;

/**
 * Rsa加解密工具
 * @author GinMeta
 */
public class RsaUtils {

    /**
     * Rsa 私钥
     */
    private static final String PRIVATE_KEY = "MIICeAIBADANBgkqhkiG9w0BAQEFAASCAmIwggJeAgEAAoGBANoZoKclT9mrT/zVG11FOrF2Uk4lWio2KRy1PRsAKICKbGtPGMLMTm9iwL8DOnRGikDhN+t0fsAv7T3ANZj5nNqqRUyNyVy1M+z9cFXwv3YAoLXLwiT7vOfDeifI7vhYD7eGNn1MVV5BvvoVIQCeLU926Mqg0VpPytgDSFzfIbd5AgMBAAECgYAKNObSy6k3+49ruqFkNQgxI+NoKtFUZmJ4KHcqm1BJ67B1OSdkfmExT0LL7Z+/r0lrDd8rErGX1uZYXHVlqlDPyL5Yf6UGe9EbKgy3OzOvlwyXse9cSzHo18wqw3vSsQ9LueUOH0qkQLj+iwJhARLH/eRmv0YwcfGKd2h/2g5aMQJBAPrH28KLwa7z60WSN6cHvr43DTYpM5ZaspXLQBwTd47+QvmrA0dB5RHUsya6x0vm1Jr5lznHxrHOM6+1IFGcmKUCQQDeo6Y3CgLKL7tXOGiVGOQwHiPoZOwssOp/e3Dtf0FGcKDFkpHyULG/tkDr0XsOruT5yJWsPRm9tNL9cBMFstdFAkEAi3CFKO88imLl9pEOkLklve4uUmcmX/ueQNv6kaHhYIjQBLKp5/0juX5i/q3sErAOlv0gsOf7+vGEm7ZCeh5wYQJBAJy4jAeQZ3hV80Upcw0JUY/Bl7s+V7X574g/p7W274cbP0D0tQHi5cyzCa42bq5eVNaz6RAujpEo2aEHpIxNYRUCQQD05T1nIO4PUCRcCoIo6ei4ZCrBzRIbFknTWGmKoE5N9TR+tLbNgOaHl7y5mUD0XXV8sl9O/6GyG/Ew3HFO+wv4";

    /**
     * 公钥
     */
    private static final String PUBLIC_KEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDaGaCnJU/Zq0/81RtdRTqxdlJOJVoqNikctT0bACiAimxrTxjCzE5vYsC/Azp0RopA4TfrdH7AL+09wDWY+ZzaqkVMjclctTPs/XBV8L92AKC1y8Ik+7znw3onyO74WA+3hjZ9TFVeQb76FSEAni1PdujKoNFaT8rYA0hc3yG3eQIDAQAB";

    private static final RSA RSA_BEAN = new RSA("RSA/ECB/PKCS1Padding", PRIVATE_KEY, PUBLIC_KEY);

    /**
     * 公钥解密
     *
     * @param text 待解密的信息
     * @return 解密后的文本
     */
    public static String decryptByPublicKey(String text) {
        return RSA_BEAN.decryptStr(text, KeyType.PublicKey);
    }

    /**
     * 私钥加密
     *
     * @param text 待加密的信息
     * @return 加密后的文本
     */
    public static String encryptByPrivateKey(String text) {
        return RSA_BEAN.encryptBase64(text, CharsetUtil.CHARSET_UTF_8, KeyType.PrivateKey);
    }

    /**
     * 私钥解密
     *
     * @param text 待解密的文本
     * @return 解密后的文本
     */
    public static String decryptByPrivateKey(String text) {
        return RSA_BEAN.decryptStr(text, KeyType.PrivateKey);
    }

    /**
     * 公钥加密
     *
     * @param text 待加密的文本
     * @return 加密后的文本
     */
    public static String encryptByPublicKey(String text) {
        return RSA_BEAN.encryptBase64(text, CharsetUtil.CHARSET_UTF_8, KeyType.PublicKey);
    }
}
